package com.frank.towerrush.activity;

import com.frank.towerrush.R;
import com.frank.towerrush.adapter.MapAdapter;
import com.frank.towerrush.util.SystemMethod;
import com.frank.towerrush.util.SystemParameter;
import com.frank.towerrush.util.SystemParameter.SystemString;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class SelectActivity extends BaseActivity
        implements OnClickListener, DialogInterface.OnClickListener, OnPageChangeListener {
    TextView txtHreat;
    TextView txtGold;
    Button btnCheat;
    Button btnStart;
    ViewPager mapSelector;

    int mapID;
    int[] mapsID;
    String[] mapsName;
    String mapName;

    @Override
    protected void init() {
        setContentView(R.layout.activity_select);

        txtHreat = (TextView) findViewById(R.id.select_heart);
        txtGold = (TextView) findViewById(R.id.select_gold);
        mapSelector = (ViewPager) findViewById(R.id.select_map);
        btnCheat = (Button) findViewById(R.id.select_btn_cheat);
        btnStart = (Button) findViewById(R.id.select_btn_start);

    }

    @Override
    protected void loadData() {
        // 读取地图id 加载到适配器
        mapsID = SystemMethod.explainIntegerArray(this, R.array.select_map);
        mapsName = getResources().getStringArray(R.array.select_map_name);
        MapAdapter adapter = new MapAdapter(this, mapsID, mapsName);
        mapSelector.setAdapter(adapter);

        // 读取金币数 和生命数
        txtHreat.setText("" + SystemParameter.hreat);
        txtGold.setText("" + SystemParameter.gold);
    }

    @Override
    protected void event() {
        mapSelector.addOnPageChangeListener(this);
        btnCheat.setOnClickListener(this);
        btnStart.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.select_btn_cheat) {
            // 加血加钱
            SystemParameter.hreat += 10;
            SystemParameter.gold += 100;
            txtHreat.setText("" + SystemParameter.hreat);
            txtGold.setText("" + SystemParameter.gold);
            return;
        }
        new AlertDialog.Builder(this).setTitle(R.string.select_dialog_tittle).setMessage(mapName)
                .setPositiveButton(R.string.ok, this).setNegativeButton(R.string.cancel, null).show();

    }

    @Override
    public void onClick(DialogInterface dialog, int which) {
        Bundle extras = new Bundle();
        extras.putInt(SystemString.PREFERENCES_GOLD, mapID);
        startActivity(GameActivity.class, extras, false);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        mapID = mapsID[position];
        mapName = mapsName[position];
    }

    @Override
    public void onPageSelected(int position) {
        // Do nothing now
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        // Do nothing now
    }

}
